package com.wh.custom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wh.custom.dao.CustomHighSeasCustomMapper;
import com.wh.custom.dao.CustomMycustomMapper;
import com.wh.custom.dto.CustomHighSeascustomQueryDto;
import com.wh.custom.dto.CustomMycustomQueryDto;
import com.wh.custom.pojo.Allocation;
import com.wh.custom.pojo.CustomHighseascustom;
import com.wh.custom.pojo.CustomMycustom;
import com.wh.custom.pojo.receiveCustomer;
import com.wh.custom.service.ICustomHighSeascustomService;
import com.wh.org.dao.IEmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务实现类
 */
@Service
public class CustomHighSeascustomServiceImpl extends ServiceImpl<CustomHighSeasCustomMapper, CustomHighseascustom> implements ICustomHighSeascustomService {

    @Autowired
    private CustomHighSeasCustomMapper customHighSeasCustomMapper;

    @Autowired
    private CustomMycustomMapper customMycustomMapper;
    @Autowired
    private IEmployeeDao iEmployeeDao;

    @Transactional
    @Override
    public IPage<CustomHighseascustom> findByPage(CustomHighSeascustomQueryDto customHighSeascustomQueryDto){
        //获取查询方式
        String QueryMethod = customHighSeascustomQueryDto.getSearchField();
        IPage<CustomHighseascustom> page = new Page<>(customHighSeascustomQueryDto.getCurrentPage(), customHighSeascustomQueryDto.getPageSize());
        QueryWrapper<CustomHighseascustom> qw = new QueryWrapper<>();
        //获取出关键字
        String keyword = customHighSeascustomQueryDto.getKeyword();
        // 根据不同的分类条件构建查询
        if ("type".equals(QueryMethod)) {
            // 如果是根据类型查询，关联 t_custom_customertype 表
            Long typeId = customHighSeasCustomMapper.selectTypeIdByTypeName(keyword);
            // 判断是否查到了类型ID
            if (typeId != null) {
                // 使用查到的类型ID进行查询
                qw.eq("type_id", typeId);
            } else {
                return page;
            }
        } else if ("source".equals(QueryMethod)) {
            Long sourceId = customHighSeasCustomMapper.selectTypeIdBySourceName(keyword);
            if (sourceId != null) {
                // 使用查到的来源ID进行查询
                qw.eq("source_id", sourceId);
            } else {
                return page;
            }
        } else if ("label".equals(QueryMethod)) {
            Long labelId = customHighSeasCustomMapper.selectTypeIdByLabelName(keyword);
            if(labelId !=null){
                qw.eq("label_id",labelId);
            }else{
                return page;
            }
        } else {
            // 如果不是分类查询，执行普通查询
            qw.like("company_name", customHighSeascustomQueryDto.getKeyword());
            qw.or();
            qw.like("customer_name", customHighSeascustomQueryDto.getKeyword());
        }
        // 组装分页数据
        page.setRecords(customHighSeasCustomMapper.findByPage(page, qw));
        page.setTotal(customHighSeasCustomMapper.selectCount(qw));

        return page;
    }
    @Override
    public void allocation(Allocation allocation) {
        List<Long> ids= allocation.getIds();
        Long salesmanId= allocation.getSalesmanId();
        for (Long id : ids) {
            customHighSeasCustomMapper.allocation(id,salesmanId);
        }
    }

    @Override
    public void recovery(receiveCustomer receiveCustomer) {
        List<Long> ids = receiveCustomer.getIds();
        Long loginId = receiveCustomer.getLoginId();//登陆人的id
        Long salesmanId= iEmployeeDao.findEmployeeIdByloginId(loginId);
        for (Long id : ids) {
            //修改选中的客户数据，将业务员id改为当前登录人
           customMycustomMapper.receiver(id,salesmanId);
        }
    }

    @Override
    public IPage<CustomHighseascustom> sort(CustomHighSeascustomQueryDto customHighSeascustomQueryDto) {
        String queryMethod = customHighSeascustomQueryDto.getSearchField();
        List<Integer> keywords = customHighSeascustomQueryDto.getKeywords();
        IPage<CustomHighseascustom> page = new Page<>(customHighSeascustomQueryDto.getCurrentPage(), customHighSeascustomQueryDto.getPageSize());
        List<CustomHighseascustom> allResults = new ArrayList<>();
        QueryWrapper<CustomHighseascustom> qw = new QueryWrapper<>();
        qw.eq("salesman_id",null);
        for (Object id : keywords) {
            // 清空之前的查询条件
            qw.clear();
            // 根据不同的分类条件构建查询
            if ("type".equals(queryMethod)) {
                Integer typeId = (Integer) id;
                qw.eq("type_id", typeId);
            } else if ("source".equals(queryMethod)) {
                Integer sourceId = (Integer) id;
                qw.eq("source_id", sourceId);
            } else if ("label".equals(queryMethod)) {
                Integer labelId = (Integer) id;
                qw.eq("label_id", labelId);
            }
            // 执行查询，并将结果添加到集合中
            List<CustomHighseascustom> partialResults = customHighSeasCustomMapper.findByPage(page, qw);
            allResults.addAll(partialResults);

        }
        // 设置总记录数
        page.setTotal(allResults.size());
        // 将所有结果设置到分页对象中
        page.setRecords(allResults);
        // 返回分页对象
        return page;
    }
    }



